package com.project.decoration.controller.link;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationLink;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationLinkService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 友情链接controller
 * @date 2025/02/12 11:57
 */
@Controller
@ResponseBody
@RequestMapping("link")
public class DecorationLinkController {

    @Autowired
    private DecorationLinkService decorationLinkService;

    /** 分页获取友情链接 */
    @PostMapping("getDecorationLinkPage")
    public Result getDecorationLinkPage(@RequestBody DecorationLink decorationLink) {
        Page<DecorationLink> page = new Page<>(decorationLink.getPageNumber(),decorationLink.getPageSize());
        QueryWrapper<DecorationLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationLink.getName()),DecorationLink::getName,decorationLink.getName())
                .like(StringUtils.isNotBlank(decorationLink.getLink()),DecorationLink::getLink,decorationLink.getLink());
        Page<DecorationLink> decorationLinkPage = decorationLinkService.page(page, queryWrapper);
        return Result.success(decorationLinkPage);
    }

    @GetMapping("getDecorationLinkList")
    public Result getDecorationLinkList() {
        List<DecorationLink> linkList = decorationLinkService.list();
        return Result.success(linkList);
    }

    /** 根据id获取友情链接 */
    @GetMapping("getDecorationLinkById")
    public Result getDecorationLinkById(@RequestParam("id")String id) {
        DecorationLink decorationLink = decorationLinkService.getById(id);
        return Result.success(decorationLink);
    }

    /** 保存友情链接 */
    @PostMapping("saveDecorationLink")
    public Result saveDecorationLink(@RequestBody DecorationLink decorationLink) {
        boolean save = decorationLinkService.save(decorationLink);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑友情链接 */
    @PostMapping("editDecorationLink")
    public Result editDecorationLink(@RequestBody DecorationLink decorationLink) {
        boolean save = decorationLinkService.updateById(decorationLink);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除友情链接 */
    @GetMapping("removeDecorationLink")
    public Result removeDecorationLink(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationLinkService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("友情链接id不能为空！");
        }
    }

}
